package com.mrzhou.bio;

import java.io.*;
import java.net.Socket;

/**
 *  Java原生Socket 客户端
 */
public class SocketClient {

    public static void main(String[] args) {
        // 服务器ip
        final String DEFAULT_SERVER_HOST = "127.0.0.1";
        // 服务端口
        final int DEFAULT_SERVER_PORT = 8888;
        try(Socket socket = new Socket(DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT)) {
            // 发送消息
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 写消息
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            BufferedReader userReader = new BufferedReader(new InputStreamReader(System.in));
            String msg;
            while (true) {
                String input = userReader.readLine();
                writer.write(input+"\n");
                writer.flush();
                // 读取服务端发送回来的消息
                msg = reader.readLine();
                // 打印发送回去的消息
                System.out.println(msg);
                if(input.equals("quit")) {
                    break;
                }
            }
        } catch (IOException e) {

        }
    }


}
